/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.windesheim.archdemo.presentationmodel;

import java.util.ArrayList;
import nl.windesheim.archdemo.dao.OrderDao;
import nl.windesheim.archdemo.model.Order;
import java.util.Date;
import java.util.List;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import nl.windesheim.archdemo.dao.GlobalInstances;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

/**
 *
 * @author re0053273
 */
public class OrderTablePresenter implements TableModel {

    private OrderDao orderDao = GlobalInstances.getOrderDao();
    /* use only through getter because of lazy loading! */
    private List<Order> orders;
    private List<TableModelListener> listeners;
    private JTable view;

    public void setView(JTable view) {
        this.view = view;
    }

    /**
     * Lazy loading of order list
     * @return 
     */
    private List<Order> getOrders() {
        if (this.orders == null) {
            this.orders = orderDao.getAll();
        }
        return this.orders;
    }

    /** gets new list of orders from dao */
    public void refresh() {
        // currently only possibility is additiof orders,
        // let's make this clear in the event so that the table keeps its selection
//        this.orders = orderDao.getAll();
        this.view.updateUI();
    }

    
    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public String getColumnName(int i) {
        switch (i) {
            case 0:
                return "id";
            case 1:
                return "Datum";
            case 2:
                return "Klant";
            case 3:
                return "Status";
            case 4:
                return "Regels";
            default:
                throw new ArrayIndexOutOfBoundsException("Order table has only 5 cols");
        }
    }

    @Override
    public int getRowCount() {
        return this.getOrders().size();
    }

    public Order getRowModel(int row) {
        return this.getOrders().get(row);
    }

    @Override
    public Object getValueAt(int row, int col) {
        Order o = this.getOrders().get(row);
        Object value = null;
        switch (col) {
            case 0:
                value = o.getId();
                break;
            case 1:
                Date d = o.getCreationDate();
                if (d != null) {
                    DateTime dt = new DateTime(d);
                    value = dt.toString("dd-MM-yy");
                }
                break;
            case 2:
                value = o.getCustomer().getName();
                break;
            case 3:
                value = o.getStatus();
                break;
            case 4:
                value = o.getOrderLines().size();
                break;
            default:
                throw new ArrayIndexOutOfBoundsException("Order table has only 5 cols");
        }
        if (value == null) {
            return "";
        }
        return value.toString();
    }

    @Override
    public Class<?> getColumnClass(int i) {
        return String.class;
    }

    @Override
    public boolean isCellEditable(int i, int i1) {
        return false;
    }

    @Override
    public void setValueAt(Object o, int i, int i1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void addTableModelListener(TableModelListener tl) {
        if( listeners == null ){
            listeners = new ArrayList<TableModelListener>(1);
            listeners.add(tl);
        }
    }

    @Override
    public void removeTableModelListener(TableModelListener tl) {
        if(listeners != null){
            listeners.remove(tl);
        }
    }
}
